<html>

	<head>
		<meta charset="utf-8" />
		<title>第一节课</title>
		<style type="text/css">
			/*去除原来样式*/
			
			* {
				padding: 0;
				margin: 0;
			}
			
			.container {
				width: 100vw;
				height: 100vh;
				display: flex;
				/*
				 * 将边框设置在中间
				 */
				align-items: center;
				justify-content: center;
			}
			
			canvas {
				width: 500px;
				height: 500px;
				border: 1px solid #333333;
			}
		</style>
	</head>

	<body>
		<div class="container">
			<canvas id="canvas" width="500" height="500"></canvas>
			<div id="">
				<input type="button" name="" id="bi" value="笔" />
				<input type="button" name="" id="shixingju" value="实心矩形" />
				<input type="button" name="" id="shixingyuan" value="实心圆" />
				<input type="button" name="" id="kongxingjuxing" value="空心矩形" />
				<input type="button" name="" id="kongxingyuan" value="空心圆" />
			</div>
		</div>

	</body>

	<script>
		window.onload = function() {
			//let oCanvas = document.getElementById('canvas');
			let oCanvas = document.getElementById("canvas");
			let pen = oCanvas.getContext("2d");

			/*document.onmousemove = function(e){
				console.dir("x="+(e.pageX-oCanvas.offsetLeft));
				console.dir("y="+(e.pageY-oCanvas.offsetTop));
			}
			
			document.onmousedown = function(e){
				console.dir("x="+(e.pageX-oCanvas.offsetLeft));
				console.dir("y="+(e.pageY-oCanvas.offsetTop));	
			}
			
			document.onmouseup = function(e){
				console.dir("x="+(e.pageX-oCanvas.offsetLeft));
				console.dir("y="+(e.pageY-oCanvas.offsetTop));
			}*/

			//笔
			let bi = document.getElementById("bi");
			bi.onclick = function() {
				pen.beginPath()
				document.onmousedown = function(e) {

					pen.moveTo((e.pageX - oCanvas.offsetLeft), (e.pageY - oCanvas.offsetTop))
					document.onmousemove = function(e) {
						//pen.beginPath()
						pen.lineTo((e.pageX - oCanvas.offsetLeft), (e.pageY - oCanvas.offsetTop))
						pen.stroke();
					}

				}

				document.onmouseup = function(e) {
					document.onmousemove = null;
				}

			}

			//实心矩形
			let shixingju = document.getElementById("shixingju");
			shixingju.onclick = function() {
				let x;
				let y;
				let m;
				let n;
				document.onmousedown = function(e) {
					x = e.pageX - oCanvas.offsetLeft;
					y = e.pageY - oCanvas.offsetTop;

					let lastX = 0,
						lastY = 0;

					document.onmousemove = function(e) {
						m = e.pageX - oCanvas.offsetLeft;
						n = e.pageY - oCanvas.offsetTop;

						pen.beginPath();
						pen.clearRect(x, y, lastX, lastY)

						//pen.clear(u)
						pen.fillRect(x, y, (m - x), (n - y));

						lastX = (m - x)
						lastY = (n - y)

						document.onmouseup = function(e) {
							document.onmousemove = null;
						}

					}

				}
			}

			//pen.fillRect(100,100,100,100);

			//空心矩形
			let kongxinju = document.getElementById("kongxingjuxing");
			kongxinju.onclick = function() {
				let x;
				let y;
				let m;
				let n;
				document.onmousedown = function(e) {
					x = e.pageX - oCanvas.offsetLeft;
					y = e.pageY - oCanvas.offsetTop;

					document.onmousemove = function(e) {
						m = e.pageX - oCanvas.offsetLeft;
						n = e.pageY - oCanvas.offsetTop;
						pen.beginPath();
						pen.clearRect(0, 0, 500, 500);
						pen.strokeRect(x, y, (m - x), (n - y));
						document.onmouseup = function(e) {
							document.onmousemove = null;
						}

					}

				}
			}

			//pen.strokeRect(100,100,100,100)

			//空心圆
			let kongxinyuan = document.getElementById("kongxingyuan");
			kongxinyuan.onclick = function() {
				let x;
				let y;
				let m;
				let n;
				document.onmousedown = function(e) {
					x = e.pageX - oCanvas.offsetLeft;
					y = e.pageY - oCanvas.offsetTop;

					document.onmousemove = function(e) {
						m = e.pageX - oCanvas.offsetLeft;
						n = e.pageY - oCanvas.offsetTop;

						pen.beginPath();
						pen.clearRect(0, 0, 500, 500)
						pen.arc(x, y, Math.abs(m - x), 0, Math.PI / 180 * 360, false);
						pen.stroke();
						document.onmouseup = function(e) {
							document.onmousemove = null;
						}

					}

				}
			}

			//pen.arc();

			//实心圆
			let shixinyuan = document.getElementById("shixingyuan");
			shixinyuan.onclick = function() {
				let x;
				let y;
				let m;
				let n;
				document.onmousedown = function(e) {
					x = e.pageX - oCanvas.offsetLeft;
					y = e.pageY - oCanvas.offsetTop;

					document.onmousemove = function(e) {
						m = e.pageX - oCanvas.offsetLeft;
						n = e.pageY - oCanvas.offsetTop;
						//pen.strokeRect(x,y,(m-x),(n-y))

						pen.beginPath();
						pen.clearRect(0, 0, 500, 500)
						pen.arc(x, y, Math.abs(m - x), 0, Math.PI / 180 * 360, false);
						pen.stroke();
						pen.fill();
						document.onmouseup = function(e) {
							document.onmousemove = null;
						}

					}

				}
			}

			//pen.arc();

		}
	</script>

</html>